package com.ytjj.bigdata.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.bigdata.dao.AdsChannelCountMapper;
import com.ytjj.bigdata.entity.AdsChannelCount;
import com.ytjj.bigdata.model.dto.AdsChannelCountDto;
import com.ytjj.bigdata.model.response.AdsChannelResponse;
import com.ytjj.common.api.CommonPageExtend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
@Slf4j
public class AdsChannelCountService {
    @Resource
    private AdsChannelCountMapper adsChannelCountMapper;

    public CommonPageExtend selectAdsChannel(AdsChannelCountDto adsChannelCountDto) {
//        AdsChannelResponse response = new AdsChannelResponse();
        PageHelper.startPage(adsChannelCountDto.getPageNum(),adsChannelCountDto.getPageSize());
        List<AdsChannelCount> list = adsChannelCountMapper.selectAdsChannel(adsChannelCountDto);

        if (CollectionUtils.isEmpty(list)) {
            return new CommonPageExtend();
        }

        list.stream().filter(item -> null != item.getProfit() && item.getProfit().compareTo(0D) > 0D).forEach(item -> {
            item.setProfit(new BigDecimal(item.getProfit()).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
        });

        CommonPageExtend pageInfo = CommonPageExtend.restPage(list);

        List<AdsChannelResponse> channelCount = adsChannelCountMapper.findAdsChannelCount(adsChannelCountDto);
        AdsChannelResponse response = channelCount.get(0);
        pageInfo.setUvCount(response.getUvCount());
        pageInfo.setNewUserCount(response.getNewUserCount());
        pageInfo.setActivationUserCount(response.getActivationUserCount());
        pageInfo.setNewUserOrderCount(response.getNewUserOrderCount());
        pageInfo.setOrderCountCount(response.getOrderCountCount());
        pageInfo.setPayRateCount(response.getPayRateCount());
        pageInfo.setIntentionOrderCount(response.getIntentionOrderCount());
        pageInfo.setPayCountCount(response.getPayCountCount());

        // 订单率 -- order_count * 100/uv
        if (response.getUvCount() > 0) {
            BigDecimal b1 = new BigDecimal(response.getOrderCountCount() * 100);
            BigDecimal b2 = new BigDecimal(response.getUvCount());
            pageInfo.setOrderRateCount(b1.divide(b2, RoundingMode.HALF_UP).doubleValue());
        }
//        pageInfo.setOrderRateCount(new BigDecimal(response.getOrderCountCount() * 100 / response.getUvCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        // 支付率 -- pay_count*100/order_count
        if (response.getOrderCountCount() > 0) {
            BigDecimal b1 = new BigDecimal(response.getPayCountCount() * 100);
            BigDecimal b2 = new BigDecimal(response.getOrderCountCount());
            pageInfo.setPayRateCount(b1.divide(b2, RoundingMode.HALF_UP).doubleValue());
        }
//        pageInfo.setPayRateCount(new BigDecimal(response.getPayCountCount() * 100 / response.getOrderCountCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
//        pageInfo.setOrderRateCount(new BigDecimal(response.getOrderRateCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
//        pageInfo.setPayRateCount(new BigDecimal(response.getPayRateCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        pageInfo.setPaymentCount(new BigDecimal(response.getPaymentCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        pageInfo.setCostCount(new BigDecimal(response.getCostCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        pageInfo.setProfitCount(new BigDecimal(response.getProfitCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        pageInfo.setAvgOrderCount(new BigDecimal(response.getAvgOrderCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

        return pageInfo;

        // order_count * 100/uv,pay_count*100/order_count


//        channelCount.stream().forEach(item -> {
//            double orderRateCount = new BigDecimal(null != item.getOrderRateCount()? item.getOrderRateCount(): 0 ).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            double payRateCount = new BigDecimal(null != item.getPayRateCount()? item.getPayRateCount() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            double paymentCount = new BigDecimal(null != item.getPaymentCount()? item.getPaymentCount() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            double costCount = new BigDecimal(null != item.getCostCount()? item.getCostCount() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            double profitCount = new BigDecimal(null != item.getProfitCount()? item.getProfitCount() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            double avgOrderCount = new BigDecimal(null != item.getAvgOrderCount()? item.getAvgOrderCount() : 0).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//            item.setOrderRateCount(orderRateCount);
//            item.setPayRateCount(payRateCount);
//            item.setPaymentCount(paymentCount);
//            item.setCostCount(costCount);
//            item.setProfitCount(profitCount);
//            item.setAvgOrderCount(avgOrderCount);
//        });
//
//        AdsChannelVo channelVo = new AdsChannelVo();
//        channelVo.setAdsChannelCountList(list);
//        channelVo.setChannelResponses(channelCount);



//        response.setUvCount(list.stream().filter(item -> null != item.getUv()).mapToInt(AdsChannelCount::getUv).sum());
//        response.setNewUserCount(list.stream().filter(item -> null != item.getNewUser()).mapToInt(AdsChannelCount::getNewUser).sum());
//        response.setActivationUserCount(list.stream().filter(item -> null != item.getActivationUser()).mapToInt(AdsChannelCount::getActivationUser).sum());
//        response.setNewUserOrderCount(list.stream().filter(item -> null != item.getNewUserOrder()).mapToInt(AdsChannelCount::getNewUserOrder).sum());
//        response.setIntentionOrderCount(list.stream().filter(item -> null != item.getIntentionOrder()).mapToInt(AdsChannelCount::getIntentionOrder).sum());
//        response.setOrderCountCount(list.stream().filter(item -> null != item.getOrderCount()).mapToInt(AdsChannelCount::getOrderCount).sum());
//        response.setPayCountCount(list.stream().filter(item -> null != item.getPayCount()).mapToInt(AdsChannelCount::getPayCount).sum());
//        response.setOrderRateCount(list.stream().filter(item -> null != item.getOrderRate()).mapToDouble(AdsChannelCount::getOrderRate).sum());
//        response.setPayRateCount(list.stream().filter(item -> null != item.getPayRate()).mapToDouble(AdsChannelCount::getPayRate).sum());
//        response.setPaymentCount(list.stream().filter(item -> null != item.getPayment()).mapToDouble(AdsChannelCount::getPayment).sum());
//        response.setCostCount(list.stream().filter(item -> null != item.getCost()).mapToDouble(AdsChannelCount::getCost).sum());
//        response.setProfitCount(list.stream().filter(item -> null != item.getProfit()).mapToDouble(AdsChannelCount::getProfit).sum());
//        response.setAvgOrderCount(list.stream().filter(item -> null != item.getAvgOrder()).mapToDouble(AdsChannelCount::getAvgOrder).sum());

//        CommonPageExtend pageInfo = CommonPageExtend.restPage(list);
//        pageInfo.setUvCount(response.getUvCount());
//        pageInfo.setNewUserCount(response.getNewUserCount());
//        pageInfo.setActivationUserCount(response.getActivationUserCount());
//        pageInfo.setNewUserOrderCount(response.getNewUserOrderCount());
//        pageInfo.setIntentionOrderCount(response.getIntentionOrderCount());
//        pageInfo.setOrderCountCount(response.getOrderCountCount());
//        pageInfo.setPayCountCount(response.getPayCountCount());
//        pageInfo.setOrderRateCount(response.getOrderRateCount());
//        pageInfo.setPayRateCount(response.getPayRateCount());
//        pageInfo.setPaymentCount(response.getPaymentCount());
//        pageInfo.setCostCount(response.getCostCount());
//        pageInfo.setProfitCount(response.getProfitCount());
//        pageInfo.setAvgOrderCount(response.getAvgOrderCount());

//        PageHelper.startPage(adsChannelCountDto.getPageNum(),adsChannelCountDto.getPageSize());

//        return channelVo;
    }
}
